<template>
  <div id="main-page-container">
    <el-container>
      <el-header class="header_jy">
        <div class="logo">
          <el-image :src="logoPath" alt="疆屿logo"></el-image>
        </div>
        <div id="system-name">疆屿后台管理系统</div>
        <div class="user-info">
          <span
            >欢迎，<span class="username-span">{{ username }}</span></span
          >
          <button @click="exit" class="exit-btn">退出登录</button>
        </div>
      </el-header>
      <el-container>
        <el-aside class="aside-menu" width="200px">
          <el-radio-group v-model="isCollapse">
            <el-radio-button :label="false">展开</el-radio-button>
            <el-radio-button :label="true">收起</el-radio-button>
          </el-radio-group>
          <el-menu
            :default-active="defaultActivePath"
            class="el-menu-vertical-demo"
            @open="handleOpen"
            @close="handleClose"
            :collapse="isCollapse"
            @select="handleMenuSelect"
          >
            <template v-for="(menu, index) in menuList">
              <!-- 渲染一级菜单，如果有子菜单则渲染为el-submenu，否则渲染为el-menu-item -->
              <el-submenu
                v-if="menu.children && menu.children.length > 0"
                :index="index + ''"
                :key="menu.path"
              >
                <template v-slot:title>
                  <i :class="menu.icon"></i>
                  <span>{{ menu.name }}</span>
                </template>
                <template v-for="subMenu in menu.children">
                  <el-menu-item :index="subMenu.path" :key="subMenu.path">
                    <i :class="subMenu.icon"></i>
                    <span>{{ subMenu.name }}</span>
                  </el-menu-item>
                </template>
              </el-submenu>
              <el-menu-item v-else :index="menu.path" :key="menu.path">
                <i :class="menu.icon"></i>
                <span>{{ menu.name }}</span>
              </el-menu-item>
            </template>
          </el-menu>
        </el-aside>
        <el-main class="content-area">
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { mapActions } from "vuex";
import { menuList } from "../data/routes.js";
export default {
  name: "home",
  data() {
    return {
      logoPath: require("@/assets/jylogo.png"),
      username: "", // 用户名
      isCollapse: false,
      menuList: menuList, 
      defaultActivePath: "/home/recommend/info",
    };
  },
  created() {
    // console.log(localStorage.getItem('token'));
    // console.log(localStorage.getItem('username'));
    this.username = localStorage.getItem("username");
  },
  methods: {
    ...mapActions(["logout"]),

    // 退出登录
    exit() {
      // console.log('退出')
      this.$confirm("确定要退出吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 调用vuex里的logout方法
          this.logout();
          // 还可以进行页面跳转，例如跳转到登录页面
          this.$router.push("/login");
        })
        .catch(() => {
          // 用户点击取消后执行的逻辑，这里可以什么都不做或者做一些提示等操作
          console.log("用户取消了退出操作");
        });
    },

    handleOpen(key, keyPath) {
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    },

    handleMenuSelect(key, keyPath) {
      // console.log(key, keyPath);
      this.defaultActivePath = key; // 当菜单被点击时，更新defaultActivePath为点击菜单的路径
      // console.log(this.$route.path)
      if (this.$route.path !== key) {
        // 只有当前路由路径和要跳转的路径不一致时，才进行路由跳转，避免重复导航
        // console.log(key);
        this.$router.push(key);
      }
    },
  },
};
</script>

<style scoped>
#main-page-container {
  /* width: 100vw; */
  height: 100vh;
  background-color: #3b89952d;
}
#main-page-container::-webkit-scrollbar{
  width: 0;
}
.header_jy {
  background-color: #85bcbb2d;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

#system-name {
  font-size: 30px;
  letter-spacing: 3px;
  font-weight: 600;
}

.username-span {
  font-size: 24px;
  margin-right: 20px;
  color: #01c6c1;
}

.exit-btn {
  border-radius: 30px;
  width: 100px;
  height: 35px;
}

.el-menu-vertical-demo:not(.el-menu--collapse) {
  min-height: 90vh;
}

 .content-area{
  height: 94vh;
  overflow-y: scroll;
  background-color: #e1fcff;
 }
 .content-area::-webkit-scrollbar{
  width: 0;
 }
</style>
